//
//  MainViewController.swift
//  MyWeibo
//
//  Created by cat on 15/7/28.
//  Copyright © 2015年 cat. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewController()
        
        print(tabBar.subviews)
        
        
    }
    /// 添所有控制器
   private func addChildViewController() {
    
        addChildViewController(HomeTableViewController(), imageName: "tabbar_home", titleName: "首页")
        addChildViewController(MessageTableViewController(), imageName: "tabbar_message_center", titleName: "消息")
        /// 添加占位控制器
        addChildViewController(UIViewController())

        addChildViewController(DiscoverTableViewController(), imageName: "tabbar_discover", titleName: "发现")
        
        addChildViewController(ProfileTableViewController(), imageName: "tabbar_profile", titleName: "我")
  
    }
    
    /// 添加子控制器
    private func addChildViewController(childController:UITableViewController, imageName:String, titleName:String) {
        
        tabBar.tintColor = UIColor.orangeColor()
        // title
        childController.title = titleName
        
        childController.tabBarItem.image = UIImage(named: imageName)
        
        let nvc = UINavigationController(rootViewController: childController)
        
        addChildViewController(nvc)
        
    }
    
    @objc private func clickComposeButton() {
        
        let vc = UserAccount.userLogon ? ComposeViewController() : OAuthViewController()
        
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
        
        
        print("click")
        
    }
    
    override func viewDidLayoutSubviews() {
        let y: CGFloat = 0
        
        let w: CGFloat = UIScreen.mainScreen().bounds.width / CGFloat((viewControllers?.count)!)
        //        print(w)
        //        print(viewControllers?.count)
        
        let h: CGFloat = tabBar.bounds.height
        
        let x: CGFloat = w * 2
        
        composeButton.frame = CGRect(x: x, y: y, width: w, height: h)
        
    }
    
    //MARK: - 懒加载tabbar中得button
    
    lazy private var composeButton : UIButton = {
        
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
    btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.tabBar.addSubview(btn)
        
        return btn
        
        }()
    
    
    
}
